﻿using System;
using System.Threading;
using System.Threading.Tasks;

namespace MoreThread
{
    internal class Program
    {
        private static AutoResetEvent auto = new AutoResetEvent(false);
        private static ManualResetEvent manualReset = new ManualResetEvent(false);
        private static CountdownEvent countdownEvent = new CountdownEvent(1000);
        private static EventWaitHandle EventWaitHandle=new EventWaitHandle(false,EventResetMode.ManualReset);
        private static Semaphore semaphore = new Semaphore(2, 5);
        private static int usingResource = 5;
        static void Main(string[] args)
        {
            #region AutoResetEvent
            // Thread thread1 = new Thread(AutoResetEventHandler);
            // Console.WriteLine("当前线程id"+Thread.CurrentThread.ManagedThreadId);
            // thread1.Start();
            // Thread.Sleep(5000);
            // auto.Set();
            //// auto.Reset();
            // Thread thread2 = new Thread(AutoResetEventHandlerTwo);
            // thread2.Start();
            // Thread.Sleep(3000);//等待3秒
            #endregion
            #region ManualResetEvent
            //Thread thread1 = new Thread(() => {
            //    manualReset.WaitOne();
            //    Console.WriteLine("最开始的执行");
            //});
            //thread1.Start();
            //Thread.Sleep(3000);
            //manualReset.Set();
            //Thread thread2 = new Thread(ManualResetEventHandler1);
            //thread2.Start();
            //manualReset.Reset();
            //Thread thread3=new Thread(ManualResetEventHandler2);
            //manualReset.Set();
            //thread3.Start();
            //manualReset.Reset();
            #endregion
            #region CountdownEvent  
            // CountReduce();
            // countdownThread.Start();
            //Thread thread = new Thread(() => {
            //    countdownEvent.Wait();
            //    Console.WriteLine("直到CountdownEvent.CurrentCount=" + countdownEvent.CurrentCount+"我才执行");
            //    //CountdownEvent.CurrentCount//当前总数
            //    //CountdownEvent.AddCount()//添加1
            //    //CountdownEvent.AddCount(10);//添加指定数量
            //    //CountdownEvent.InitialCount//总数
            //    //CountdownEvent.Reset()//设置为InitialCount初始值
            //    //CountdownEvent.Reset(100)//设置为指定初始值
            //});
            //thread.Start();
            #endregion
            #region EventWaitHandle
            //var num = Interlocked.Exchange(ref usingResource, 0);多线程中进行原子操作，两个值进行比较，并且将值保存到第三方变量中
            //Console.WriteLine(num);
            //Thread thread1 = new Thread(EventWaitHandle1);
            //Thread thread2 = new Thread(EventWaitHandle2);
            //Thread thread3 = new Thread(EventWaitHandle3);
            //thread1.Start();
            //thread2.Start();
            //thread3.Start();
            //EventWaitHandle.Set();
            //Thread thread4 = new Thread(EventWaitHandl4);
            //Thread.Sleep(1000);
            //thread4.Start();
            //EventWaitHandle.Reset();
            //EventWaitHandle.Set();
            // thread4.Start();
            #endregion
            #region Semaphore
            for (var i = 0; i < 12; i++)
            {
                Thread thread = new Thread(new ParameterizedThreadStart(Semaphorehandle));
                thread.Start(i);
            }
            //Console.WriteLine(semaphore.Release(2));
            #endregion
        }


        #region AutoResetEvent
        private static void AutoResetEventHandler()
        {
            Console.WriteLine("当前线程id" + Thread.CurrentThread.ManagedThreadId);
            auto.WaitOne();
            Console.WriteLine("等待一秒后执行");
        }
        private static void AutoResetEventHandlerTwo()
        {
            auto.WaitOne();
            Console.WriteLine("我是第二个等待执行");
        }
        #endregion
        #region ManualResetEvent
        private static void ManualResetEventHandler1()
        {
            manualReset.WaitOne();
            Console.WriteLine("第一次等待执行");
        }
        private static void ManualResetEventHandler2()
        {
            manualReset.WaitOne();
            Console.WriteLine("第二次等待执行");
        }
        #endregion
        #region CountdownEvent  
        private static async Task CountReduce()
        {
           await Task.Run(async () => {
               for(var i = 0; i < 1000; i++)
                {
                    await Task.Delay(10);//休眠100毫秒--等到100毫秒
                   //if (countdownEvent.CurrentCount < 10)
                   //{
                   //    countdownEvent.Reset(100);
                   //    CountReduce();
                   //}
                   countdownEvent.Signal();
                    Console.WriteLine("当前总数"+countdownEvent.CurrentCount);
                }
            });
        }
        #endregion
        #region EventWaitHandle
        private static void EventWaitHandle1()
        {
            EventWaitHandle.WaitOne();
            Thread.Sleep(1000);
            Console.WriteLine("我是第1个EventWaitHandle");
        }
        private static void EventWaitHandle2()
        {
            EventWaitHandle.WaitOne();
            Thread.Sleep(2000);
            Console.WriteLine("我是第2个EventWaitHandle");
        }
        private static void EventWaitHandle3()
        {
            EventWaitHandle.WaitOne();
            Thread.Sleep(3000);
            Console.WriteLine("我是第3个EventWaitHandle");
        }
        private static void EventWaitHandl4()
        {
            Thread.Sleep(3000);
            EventWaitHandle.WaitOne();
            Console.WriteLine("我是第4个EventWaitHandle");
        }
        #endregion
        #region Semaphore
        private static void Semaphorehandle(Object i)
        {
            semaphore.WaitOne();
            Console.WriteLine((int)i + "进入了线程");
            Thread.Sleep(2000);
            Console.WriteLine((int)i + "准备离开线程");
            if ((int)i >1)
            {
                Console.WriteLine(semaphore.Release());
                return;
            }
            semaphore.Release(2);
        }
        #endregion

    }
}
